/******************************************************************/
/*** STATA Code                                                 ***/
/*** Review of Corporate Finance Studies                        ***/
/*** "Unintended Consequences of Macroprudential Regulation"    ***/
/*** by                                                         ***/  
/*** Erik Berwart, Mauricio Larrain, and Patricio Valenzuela    ***/
/******************************************************************/

set more off 
clear all
cd "C:\Users\Results"

* Global
global a  "edad ma income sexd dep i.comuna i.prof if post==0"
global a1 "edad ma income sexd dep"

global b  "edad ma income sexd dep edad_ma edad_income edad_sexd ma_income ma_sexd income_sexd  i.comuna i.prof if post==0"
global b1 "edad ma income sexd     edad_ma edad_income edad_sexd ma_income ma_sexd income_sexd"


/*******************************/
/*** CMF ADMINISTRATIVE DATA ***/
/*******************************/

use "C:\Users\CMF_Administrative_Data.dta", replace


/***********************************/
/*** Table 1: Summary Statistics ***/
/***********************************/

reghdfe cudesm12_1 	pltvn80_post pltvn80 $b1, absorb(cy prof) cluster(comuna)
keep if e(sample)
foreach x in ltvn ltvn80 cudesm12_1  age ma sex income dep debt_c int_m h_s3 {
sum `x', detail
}


/**************************************/
/*** Table 2: Predicting LTV Ratios ***/
/**************************************/

reghdfe ltvn edad ma  sexd income dep if post==0, absorb(comuna prof) 
estimates store model1

reghdfe ltvn edad ma  sexd income dep edad_ma edad_income edad_sexd ma_income ma_sexd income_sexd if post==0, absorb(comuna prof)
predict pltvn80
gen pltvn80_post=pltvn80*post 
estimates store model2

esttab model1 model2 using Table2.tex, title("Table 2: Regression Results") label tex stats(r2 N) star(* 0.10 ** 0.05 *** 0.01) replace


/******************************************************************/
/*** Table 3: Differences in observables according to LTV ratio ***/
/******************************************************************/

preserve
sum dep, detail
reghdfe cudesm12_1 	pltvn80_post pltvn80 $b1, absorb(cy prof) cluster(comuna)
keep if e(sample)
keep if cudesm12_1!=.

ttest edad, by(pltvn80)
ttest ma, by(pltvn80) 
ttest sexd, by(pltvn80)
ttest income, by(pltvn80) 
ttest dep, by(pltvn80)
restore


/*****************************************************************/
/*** Table 4: Loan to Value and Fraction of Loans with LTV>80% ***/
/*****************************************************************/

sum ltvn if post==0 & pltvn80>0.8
sum ltvn if post==1 & pltvn80>0.8

sum ltvn if post==0 & pltvn80<=0.8
sum ltvn if post==1 & pltvn80<=0.8

sum ltvn80 if post==0 & pltvn80>0.8
sum ltvn80 if post==1 & pltvn80>0.8

sum ltvn80 if post==0 & pltvn80<=0.8
sum ltvn80 if post==1 & pltvn80<=0.8


/**************************************************************************/
/*** Table 5: Impact of Regulation on Origination of High-LTV Mortgages ***/
/**************************************************************************/

reghdfe ltvn80 		pltvn80_post pltvn80 $a1, absorb(comuna prof) cluster(comuna) 
estimates store model1

reghdfe ltvn80 		pltvn80_post pltvn80 $a1, absorb(year prof) cluster(comuna) 
estimates store model2

reghdfe ltvn80 		pltvn80_post pltvn80 $a1, absorb(cy prof) cluster(comuna) 
estimates store model3

esttab model1 model2 model3 using Table5.tex, title("Table 5: Regression Results") label tex  stats(r2 N) star(* 0.10 ** 0.05 *** 0.01) replace


/*************************************************************/ 
/*** Table 6: Impact of Regulation on Mortgage Delinquency ***/
/*************************************************************/

reghdfe cudesm12_1 	pltvn80_post pltvn80 $a1, absorb(cy prof) cluster(comuna)
estimates store model1

reghdfe cudesm12_2 	pltvn80_post pltvn80 $a1 , absorb(cy prof) cluster(comuna)
estimates store model2

reghdfe cudesm12_4 	pltvn80_post pltvn80 $a1 , absorb(cy prof) cluster(comuna)
estimates store model3

esttab model1 model2 model3 using Table6.tex, title("Table 6: Regression Results") label tex stats(r2 N) star(* 0.10 ** 0.05 *** 0.01) replace


/*********************************************************/ 
/*** Table 7: Impact of Regulation on Mortgage Pricing ***/
/*********************************************************/

reghdfe int_m 	pltvn80_post pltvn80 $a1, absorb(cy prof) cluster(comuna)
estimates store model1

esttab model1  using Table7.tex, title("Table 7: Regression Results") label tex stats(r2 N) star(* 0.10 ** 0.05 *** 0.01) replace


/****************************************************************************/
/*** Table 8: Bank Capital Slack and Anticipation in High-LTV Origination ***/
/****************************************************************************/

reghdfe ltvn80  d2015 c04_irs_d2015 if year==2014 | year==2015, absorb(bank) cluster(comuna)
estimates store model1

reghdfe ltvn80  d2015 c04_irs_d2015 if year==2014 | year==2015, absorb(bank comuna) cluster(comuna)
estimates store model2

reghdfe ltvn80   c04_irs_d2015 if year==2014 | year==2015, absorb(bank cy) cluster(comuna)
estimates store model3

esttab model1 model2  model3 using Table8.tex, title("Table 8: Regression Results") label tex stats(r2 N) star(* 0.10 ** 0.05 *** 0.01) replace


/*****************************************************/
/*** Table 9: Mortgage Delinquency: Placebo  Tests ***/
/*****************************************************/

reghdfe cudesm12_2 	pltvn80_post2 pltvn80 $a1 if year<2015, absorb(cy prof) cluster(comuna)
estimates store model2

reghdfe cudesm12_2 	pltvn80_post3 pltvn80 $a1 if year<2015, absorb(cy prof) cluster(comuna)
estimates store model3

reghdfe cudesm12_2 	pltvn80_post4 pltvn80 $a1 if year<2015, absorb(cy prof) cluster(comuna)
estimates store model4

esttab  model4 model3 model2 using Table9.tex, title("Table 9: Regression Results") label tex stats(r2 N) star(* 0.10 ** 0.05 *** 0.01) replace


/************************************************************************************/
/*** Table 10: Mortgage Delinquency Around Bank-Level ImplementationBank-specific ***/
/************************************************************************************/

reghdfe cudesm12_1 	pltvn80_dummy pltvn80 $a1, absorb(cy prof) cluster(comuna)
estimates store model1

reghdfe cudesm12_2 	pltvn80_dummy pltvn80 $a1 , absorb(cy prof) cluster(comuna)
estimates store model2

reghdfe cudesm12_4 	pltvn80_dummy pltvn80 $a1 , absorb(cy prof) cluster(comuna)
estimates store model3

esttab model1 model2 model3 using Table10.tex, title("Table 10: Regression Results") label tex stats(r2 N) star(* 0.10 ** 0.05 *** 0.01) replace


/**********************************************************************************/
/*** Table 11: Mortgage Delinquency by LTV Bins Around the Regulatory Threshold ***/
/**********************************************************************************/

reghdfe cudesm12_1 	pltvn80_post pltvn80 $a1 if ltvn<0.9 & ltvn>0.7, absorb(cy prof) cluster(comuna)
estimates store model1

reghdfe cudesm12_1 	pltvn80_post pltvn80 $a1 if ltvn<0.88 & ltvn>0.72, absorb(cy prof) cluster(comuna)
estimates store model2

reghdfe cudesm12_1 	pltvn80_post pltvn80 $a1 if ltvn<0.86 & ltvn>0.74, absorb(cy prof) cluster(comuna)
estimates store model3

reghdfe cudesm12_1 	pltvn80_post pltvn80 $a1 if ltvn<0.84 & ltvn>0.76, absorb(cy prof) cluster(comuna)
estimates store model4

esttab model1 model2 model3 model4 using Table11.tex, title("Table 11: Regression Results") label tex stats(r2 N) star(* 0.10 ** 0.05 *** 0.01) replace


/*****************************************************/
/*** Table 12: Ruling out Alternative Explanations ***/
/*****************************************************/

reghdfe cudesm12_1 	pltvn80_post pltvn80_house pltvn80 $a1, absorb(cy prof) cluster(comuna)
estimates store model1

reghdfe cudesm12_1 	pltvn80_post pltvn80_cooper pltvn80 $a1, absorb(cy prof) cluster(comuna)
estimates store model2

reghdfe cudesm12_1 	pltvn80_post pltvn80_unemployment pltvn80 $a1, absorb(cy prof) cluster(comuna)
estimates store model3

esttab model1 model2 model3 using Table12.tex, title("Table 12: Regression Results") label tex stats(r2 N) star(* 0.10 ** 0.05 *** 0.01) replace


 /******************************************************/
 /*** Table 13: Impact of Regulation Appraised Value ***/
 /******************************************************/
 
reghdfe lappra 		pltvn80_post pltvn80 $a1, absorb(cy prof) cluster(comuna) 
estimates store model1

reghdfe lappra 		pltvn80_post pltvn80 $a1 if densidad<2977, absorb(cy prof) cluster(comuna) 
estimates store model2

reghdfe lappra 		pltvn80_post pltvn80 $a1 if densidad>=2977, absorb(cy prof) cluster(comuna) 
estimates store model3

reghdfe lappra 		pltvn80_post pltvn80_post_density pltvn80 $a1, absorb(cy prof) cluster(comuna) 
estimates store model4

esttab model1 model2 model3 model4 using Table13.tex, title("Table 13: Regression Results") label tex stats(r2 N) star(* 0.10 ** 0.05 *** 0.01) replace
 

/**************************************************************************/
/*** Table 14: Trnasmission Channels Underlying the Rise of Delinquency ***/
/**************************************************************************/

reghdfe dep 		pltvn80_post pltvn80 edad ma income sexd, absorb(cy prof) cluster(comuna)
estimates store model1

reghdfe debt_c 		pltvn80_post pltvn80 $a1, absorb(cy prof) cluster(comuna)
estimates store model2

reghdfe h_s3 		pltvn80_post pltvn80 $a1, absorb(cy prof) cluster(comuna)
estimates store model3
esttab model1 model2 model3 using Table14.tex, title("Table 14: Regression Results") label tex stats(r2 N) star(* 0.10 ** 0.05 *** 0.01) replace


/****************************************************************************************************/
/*** Table A.2: Impact of Regulation on Mortgage Delinquency - Block Bootstapping Standard Errors ***/
/****************************************************************************************************/
 
bootstrap _b, reps(100) seed(12345) cluster(comuna): areg cudesm12_1 	pltvn80_post pltvn80 $a1, absorb(cy) cluster(comuna)
estimates store model1

bootstrap _b, reps(100) seed(12345) cluster(comuna): areg cudesm12_2 	pltvn80_post pltvn80 $a1, absorb(cy) cluster(comuna)
estimates store model2

bootstrap _b, reps(100) seed(12345) cluster(comuna): areg cudesm12_4 	pltvn80_post pltvn80 $a1, absorb(cy) cluster(comuna)
estimates store model3
esttab model1 model2 model3 using TableA2.tex, title("Table A2: Regression Results") label tex stats(r2 N) star(* 0.10 ** 0.05 *** 0.01) replace


/******************************************************************/
/*** Table A.3: Impact of Regulation on Mortgage Borrowers' Age ***/
/******************************************************************/

reghdfe edad 	pltvn80_post pltvn80 ma income sexd dep, absorb(cy prof) cluster(comuna)
estimates store model1
esttab model1 model2 model3 using TableA3.tex, title("Table A3: Regression Results") label tex stats(r2 N) star(* 0.10 ** 0.05 *** 0.01) replace



/***************/
/*** FIGURES ***/
/***************/

/*********************************************/
/*** Data Figure 1: Evolution of LTV ratio ***/
/*********************************************/

sum ltvn, detail
preserve
collapse (median) ltvn, by(year month)
restore


/*********************************************************************************************************/
/*** Figure 2: Density of LTV Ratios Pre- amd Post-Regulation: All Borrowers and Constraines Borrowers ***/
/*********************************************************************************************************/

preserve 
drop if ltvn>1.05
drop if ltvn<0.2
twoway   (histogram ltvn if year<2015, bin(70) color(orange%50)) (histogram ltvn if year>=2015, bin(70) color(none) lcolor(black) lwidth(thin)), yscale(range(0 0.13)) xtitle(Loan to Value ratio) legend(position(6) cols(2) label(1 "Pre") label(2 "Post")) graphregion(color(ebblue*.1))
restore

preserve 
keep if pltvn80==1
drop if ltvn>1.05
drop if ltvn<0.2
twoway   (histogram ltvn if year<2015, bin(70) color(orange%50)) (histogram ltvn if year>=2015, bin(70) color(none) lcolor(black) lwidth(thin)), yscale(range(0 0.13)) xtitle(Loan to Value ratio) legend(position(6) cols(2) label(1 "Pre") label(2 "Post")) graphregion(color(ebblue*.1))
restore


/******************************************************************************************/
/*** Data Figure 3: Evolution of Mortgage Delinquency for Treated and Control Borrowers ***/
/******************************************************************************************/

sum ltvn, detail
preserve
collapse (median) cudesm12_1, by(year_quar)
restore


/********************************************************************************/
/*** Data Figure 4: Dynamic Effects of the Regulation on Mortgage Delinquency ***/
/********************************************************************************/

reghdfe cudesm12_1 pltvn80 pltvn80_d1 pltvn80_d2 pltvn80_d3 pltvn80_d5 pltvn80_d6 pltvn80_d7 pltvn80_d8 $a1, absorb(cy prof) cluster(comuna)cluster(comuna) 

